package com.apps.ftapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.model.LessonDetailItemModel;
import com.apps.ftapp.model.SubjectDetailModel;
import com.apps.ftapp.util.DateUtil;
import com.apps.ftapp.util.Tools;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lly on 2017/10/11.
 */

public class LessonDetailAdapter extends BaseAdapter {


    private Context context;
    private List<LessonDetailItemModel> dataList = new ArrayList<>();
    private LayoutInflater layoutInflater;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;
    private String type;


    public LessonDetailAdapter(Context context, List<LessonDetailItemModel> dataList, String type) {
        this.context = context;
        this.dataList = dataList;
        this.type = type;
        layoutInflater = LayoutInflater.from(this.context);

        //显示图片的配置  
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));

    }


    public void addRes(List data) {
        if (data != null) {
            this.dataList.clear();
            this.dataList.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        if (null != dataList && dataList.size() > 0) {
            return dataList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LessonDetailItemModel bean = dataList.get(i);
        ViewHolder viewHolder;
        if (view == null) {
            view = this.layoutInflater.inflate(R.layout.activity_lesson_detail_listview_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.lessonDetailTitle.setText(bean.getTitle());
        if (type.equals("normal")) {
            viewHolder.lessonDetailTime.setText("");
        } else {
            viewHolder.lessonDetailTime.setText(bean.getStartTime() + "至" + bean.getEndTime());
        }

        //数组 shixue shixue[if]为1则代表有试学 0则没有 来控制是否显示试学按钮 shixue[lesson]为试学列表
        // （其中type 为状态 live 直播；video 录播,weikaike未开课 未开课时点击提示wkktext。直播和回放分别根据roomId进入不同的页面）
        if (Tools.isNull(bean.getShixue()).equals("1")) {
            if (i == 0) {

                viewHolder.shixue.setVisibility(View.VISIBLE);
                viewHolder.img.setVisibility(View.GONE);
            } else {
                viewHolder.shixue.setVisibility(View.GONE);
                viewHolder.img.setVisibility(View.VISIBLE);
            }

        } else {

            viewHolder.shixue.setVisibility(View.GONE);
            viewHolder.img.setVisibility(View.VISIBLE);
        }


        return view;
    }


    class ViewHolder {
        TextView lessonDetailTitle;
        TextView lessonDetailTime;
        TextView shixue;
        ImageView img;

        public ViewHolder(View view) {
            lessonDetailTitle = view.findViewById(R.id.lesson_detail_title);
            lessonDetailTime = view.findViewById(R.id.lesson_detail_time);
            shixue = view.findViewById(R.id.shixue111);
            img = view.findViewById(R.id.img);
        }
    }

}
